/**
 * 48. 旋转图像
 * https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=top-100-liked
 */
class Solution2 {
    public void rotate(int[][] matrix) {
        int n = matrix.length, m = matrix[0].length;
        int top = 0, bottom = n - 1, left = 0, right = m - 1;

        //水平翻转
        while (top < bottom) {
            for (int i = 0; i < n; i++) {
                swap(matrix, new int[]{bottom, i}, new int[]{top, i});
            }
            top++;
            bottom--;
        }
        //重新初始化各顶点
//        top = 0;
//        bottom = n - 1;
//        left = 0;
//        right = m - 1;
//        //对角线翻转
//        for (int i = 0; i < n; i++) {
//            top = i + 1; left = i + 1;
//            while (top <= bottom && left <= right) {
//                //列                   //行
//                // matrix[top++][i] = matrix[i][left++]
//                swap(matrix, new int[]{top++, i}, new int[]{i, left++});
//            }
//        }

        //主对角线翻转，i j下标互换即可
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < i; j++) {
                swap(matrix, new int[]{i, j}, new int[]{j, i});
            }
        }
    }


    public void swap(int[][] matrix, int[] a, int[] b) {
        int i = a[0], j = a[1];
        int x = b[0], y = b[1];

        int tmp = matrix[i][j];
        matrix[i][j] = matrix[x][y];
        matrix[x][y] = tmp;
    }
}